/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.supinfo.youfood.web.filter;

import com.supinfo.youfood.entity.Employe;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author TaKeO
 */
@WebFilter(filterName = "ServeurFilter", urlPatterns = {"/serveur/*"})
public class ServeurFilter implements Filter {
  
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        
        if(httpRequest.getSession().getAttribute("currentEmp") == null || ((Employe)httpRequest.getSession().getAttribute("currentEmp")).getType_employe().getDroits() != 222 ) 
        {
            httpResponse.sendRedirect(request.getServletContext().getContextPath() + "/");
        } 
        else 
        {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {
        // Do nothing
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // Do nothing
    }
}
